package com.woniu.sharepark.leasesystem.common.controller;

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.leasesystem.common.dao.OrderFormDAO;
import com.woniu.sharepark.leasesystem.common.exception.BussinessException;
import com.woniu.sharepark.leasesystem.common.service.OrderFormService;
import com.woniu.sharepark.leasesystem.lessor.dao.CarParkDAO;
import com.woniu.sharepark.leasesystem.lessor.pojo.CarPark;
import com.woniu.sharepark.leasesystem.lessor.service.CarParkService;
import com.woniu.sharepark.util.websocket.MsgWebSocketServer;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author DELL
 * @date 2019/6/10 12:39
 * version v1.0
 */
@Controller
@RequestMapping
@Validated
public class TestController {

    @Resource
    private OrderFormDAO orderFormDAO;

    @Resource
    private CarParkService carParkService;

    @Resource
    private OrderFormService orderFormService;

    @Resource
    private CarParkDAO carParkDAO;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/create")
    @ResponseBody
    public void createOrderNumber(){
        CarPark carPark = carParkDAO.selectByPrimaryKey(1);
        System.out.println(carPark);
        JsonResult rows = orderFormService.createOrderNumber(carPark);
        System.out.println(rows);
    }

    @RequestMapping("/payTest")
    @ResponseBody
    public void  payTest(){
        Session session  = SecurityUtils.getSubject().getSession();
        session.setAttribute("17775514495orderNumber","79a5818b86104ce60e9ad9427d279151");
        JsonResult rows = orderFormService.payOrder(new Date(),1);
        System.out.println(rows);
    }



    //页面请求
    @GetMapping("/socket/{cid}")
    public ModelAndView socket(@PathVariable String cid) {
        ModelAndView mav=new ModelAndView("/lzjtesthtml/webSocket");
        mav.addObject("cid", cid);
        System.out.println(cid);
        return mav;
    }

  /*  //推送数据接口
    @RequestMapping("/socket/push/{cid}")
    public void pushToWeb(@PathVariable String cid,String message) {
        try {
            WebSocketServer.sendInfo("你好",cid);
        } catch (IOException e) {
            e.printStackTrace();

        }

    }*/

    /*@RequestMapping(value="/pushVideoListToWeb")
    @ResponseBody
    public  Map<String,Object> pushVideoListToWeb( Map<String,Object> param) {
        System.out.println("ss");
        Map<String,Object> result =new HashMap<String,Object>();

        try {
            WebSocketServer.sendInfo("有新客户呼入,sltAccountId:");
            result.put("operationResult", true);
        }catch (IOException e) {
            result.put("operationResuddlt", true);
        }
        return result;
    }*/

/*    @RequestMapping("/sendAll")
    @ResponseBody
    public String sendAll() throws IOException {
        for (int i=0;i<=3;i++){
            System.out.println("ss");
            WebSocketServer.sendInfo("你好吗");
        }

        return "success";
    }*/

    @RequestMapping("sendmsg")
    @ResponseBody
    public String sendmsg(){
        //第一个参数 :msg 发送的信息内容
        //第二个参数为用户长连接传的用户人数
        MsgWebSocketServer.sendToUser("我是谁","xw","");
        return "success";
    }

    /**
     * 推送给所有在线用户
     * @return
     */
    @RequestMapping("sendAll")
    @ResponseBody
    public String sendAll(){
        MsgWebSocketServer.sendToAll("车位号1111待审核","");
        return "success";
    }

    @RequestMapping("/testNull")
    @ResponseBody
    public String testNull(@NotNull(message="缺少参数testData")String testData){
        System.out.println(testData);
        return testData;
    }

    @RequestMapping("/testRedis")
    @ResponseBody
    public String  testRedis(){
        redisTemplate.opsForValue().set("keyTest","123",10, TimeUnit.SECONDS);
        return "success";
    }

    public static void main(String[] args) {
         double add = add(2.0,25.5999);
        DecimalFormat decimalFormat = new DecimalFormat("######0.00");
        String ss = decimalFormat.format(add);
        System.out.println(ss);
    }

    public static double add(double a1, double b1) {
               BigDecimal a2 = new BigDecimal(Double.toString(a1));
               BigDecimal b2 = new BigDecimal(Double.toString(b1));
               return a2.add(b2).doubleValue();
    }

    @RequestMapping("/testException")
    @ResponseBody
    public  String testException(){
        throw new BussinessException(100,"密码错误");
        //return "success";
    }

}

